#!/bin/sh
# GScore 仓库镜像同步还原工具

set -e

# 默认插件目录
DEFAULT_PLUGINS_DIR="gsuid_core/plugins"
PLUGINS_DIR="${1:-$DEFAULT_PLUGINS_DIR}"

echo "开始还原 git remote url 到原始地址..."
echo "插件目录: $PLUGINS_DIR"
echo ""

# URL 映射函数 (POSIX sh 兼容，移除 local 关键字)
get_target_url() {
    name=$1
    case $name in
        gsuid_core) echo "https://github.com/Genshin-bots/gsuid_core" ;;
        XutheringWavesUID) echo "https://github.com/Loping151/XutheringWavesUID" ;;
        RoverSign) echo "https://github.com/tyql688/RoverSign" ;;
        DNAUID) echo "https://github.com/tyql688/DNAUID" ;;
        GenshinUID) echo "https://github.com/KimigaiiWuyi/GenshinUID" ;;
        StarRailUID) echo "https://github.com/baiqwerdvd/StarRailUID" ;;
        ZZZeroUID) echo "https://github.com/ZZZure/ZZZeroUID" ;;
        DeltaUID) echo "https://github.com/Agnes4m/DeltaUID" ;;
        SayuStock) echo "https://github.com/KimigaiiWuyi/SayuStock" ;;
        BlueArchiveUID) echo "https://github.com/KimigaiiWuyi/BlueArchiveUID" ;;
        VAUID) echo "https://github.com/Agnes4m/VAUID" ;;
        MajsoulUID) echo "https://github.com/KimigaiiWuyi/MajsoulUID" ;;
        WzryUID) echo "https://github.com/KimigaiiWuyi/WzryUID" ;;
        LOLegendsUID) echo "https://github.com/KimigaiiWuyi/LOLegendsUID" ;;
        CS2UID) echo "https://github.com/Agnes4m/CS2UID" ;;
        ScoreEcho) echo "https://github.com/Loping151/ScoreEcho" ;;
        TodayEcho) echo "https://github.com/Loping151/TodayEcho" ;;
        *) echo "" ;;
    esac
}

TOTAL=0
SUCCESS=0
SKIPPED=0

# 插件列表（POSIX sh 兼容的写法）
PLUGINS="
XutheringWavesUID
RoverSign
DNAUID
GenshinUID
StarRailUID
ZZZeroUID
DeltaUID
SayuStock
BlueArchiveUID
VAUID
MajsoulUID
WzryUID
LOLegendsUID
CS2UID
ScoreEcho
TodayEcho
"

# 处理 gsuid_core 本体
if [ -d ".git" ]; then
    current_url=$(git remote get-url origin 2>/dev/null || echo "")

    if [ -z "$current_url" ]; then
        echo "  ⊘ 无 origin remote"
        SKIPPED=$((SKIPPED + 1))
    else
        target_url=$(get_target_url "gsuid_core")

        echo "  当前: $current_url"
        echo "  目标: $target_url"

        case "$current_url" in
            *github.com*)
                if echo "$current_url" | grep -q "gitcode"; then
                    if git remote set-url origin "$target_url" 2>/dev/null; then
                        echo "  ✓ 成功"
                        SUCCESS=$((SUCCESS + 1))
                    else
                        echo "  ✗ 失败"
                    fi
                else
                    echo "  ✓ 已是原始地址"
                    SUCCESS=$((SUCCESS + 1))
                fi
                ;;
            *gitcode.com*|*cnb.cool*)
                if git remote set-url origin "$target_url" 2>/dev/null; then
                    echo "  ✓ 成功"
                    SUCCESS=$((SUCCESS + 1))
                else
                    echo "  ✗ 失败"
                fi
                ;;
            *)
                echo "  ⊘ 未知地址，跳过"
                SKIPPED=$((SKIPPED + 1))
                ;;
        esac
    fi
else
    echo "  ⊘ 非 git 仓库或目录不存在"
    SKIPPED=$((SKIPPED + 1))
fi
echo ""

# 检查并收集已存在的插件
EXISTING_PLUGINS=""
for plugin in $PLUGINS; do
    plugin_path="$PLUGINS_DIR/$plugin"
    if [ -d "$plugin_path" ]; then
        if [ -z "$EXISTING_PLUGINS" ]; then
            EXISTING_PLUGINS="$plugin"
        else
            EXISTING_PLUGINS="$EXISTING_PLUGINS
$plugin"
        fi
    fi
done

if [ -z "$EXISTING_PLUGINS" ]; then
    echo "未找到任何插件目录，请检查路径: $PLUGINS_DIR"
    exit 0
fi

PLUGIN_COUNT=$(echo "$EXISTING_PLUGINS" | wc -l | tr -d ' ')
echo "发现 $PLUGIN_COUNT 个插件目录，开始处理..."
echo ""

# 处理插件
for plugin in $EXISTING_PLUGINS; do
    plugin_path="$PLUGINS_DIR/$plugin"

    echo "处理插件: $plugin"

    if [ ! -d "$plugin_path/.git" ]; then
        echo "  ⊘ 非 git 仓库"
        SKIPPED=$((SKIPPED + 1))
        echo ""
        continue
    fi

    cd "$plugin_path"
    current_url=$(git remote get-url origin 2>/dev/null || echo "")

    if [ -z "$current_url" ]; then
        echo "  ⊘ 无 origin remote"
        SKIPPED=$((SKIPPED + 1))
        cd - >/dev/null 2>&1
        echo ""
        continue
    fi

    target_url=$(get_target_url "$plugin")
    echo "  当前: $current_url"
    echo "  目标: $target_url"

    case "$current_url" in
        *github.com*)
            if echo "$current_url" | grep -q "gitcode"; then
                # GitCode 镜像地址，还原为 GitHub
                if git remote set-url origin "$target_url" 2>/dev/null; then
                    echo "  ✓ 成功"
                    SUCCESS=$((SUCCESS + 1))
                else
                    echo "  ✗ 失败"
                fi
            else
                # 已经是 GitHub 原始地址
                echo "  ✓ 已是原始地址"
                SUCCESS=$((SUCCESS + 1))
            fi
            cd - >/dev/null 2>&1
            echo ""
            continue
            ;;
        *gitcode.com*|*cnb.cool*)
            # GitCode 镜像地址，还原为 GitHub
            if git remote set-url origin "$target_url" 2>/dev/null; then
                echo "  ✓ 成功"
                SUCCESS=$((SUCCESS + 1))
            else
                echo "  ✗ 失败"
            fi
            cd - >/dev/null 2>&1
            echo ""
            continue
            ;;
        *)
            # 未知地址，跳过
            echo "  ⊘ 未知地址，跳过"
            SKIPPED=$((SKIPPED + 1))
            ;;
    esac

    cd - >/dev/null 2>&1
    echo ""
done

TOTAL=$((PLUGIN_COUNT + 1))
echo "========================================"
echo "完成！总计: $TOTAL | 成功: $SUCCESS | 跳过: $SKIPPED"
echo "========================================"
